package com.cqhilink.iot.jobstandardmanagement.services.innerservice.impl.device;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.cqhilink.api.common.utils.UniqueUtil;
import com.cqhilink.api.common.utils.date.DateStyle;
import com.cqhilink.api.common.utils.date.DateUtil;
import com.cqhilink.api.common.utils.enums.RespCodeEnum;
import com.cqhilink.iot.jobstandardmanagement.common.calculator.Time;
import com.cqhilink.iot.jobstandardmanagement.common.calculator.TimeCalc;
import com.cqhilink.iot.jobstandardmanagement.facade.model.*;
import com.cqhilink.iot.jobstandardmanagement.facade.service.device.JsmDeviceService;
import com.cqhilink.iot.jobstandardmanagement.facade.utils.BackMessage;
import com.cqhilink.iot.jobstandardmanagement.dao.mapper.DeviceMapper;
import com.cqhilink.iot.jobstandardmanagement.dao.mapper.DeviceValidateMapper;
import com.cqhilink.iot.jobstandardmanagement.facade.service.device.JsmDeviceValidateService;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.*;

/**
 * 设备校验记录填写
 * @author 黄学乾 778142659@qq.com
 * @create 2017-08-03 14:21
 **/
public class JsmDeviceValidaeServiceImpl implements JsmDeviceValidateService {
    public Logger logger = LoggerFactory.getLogger(this.getClass());
    @Autowired
    private DeviceValidateMapper deviceValidateMapper ;
    @Autowired(required = false)
    private DeviceMapper deviceMapper ;
    @Autowired(required = false)
    private JsmDeviceService jsmDeviceService ;

    /**
     * @param deviceValidate 设备校验周期
     * @param employeeVo 操作人
     * @return
     * @throws Exception
     */
    @Override
    public Map<String, Object> saveDeviceValidate(DeviceValidate deviceValidate, EmployeeVo employeeVo) throws Exception {
        Map<String,Object> valdateMap = new HashMap<>();
        logger.info("校验记录保存：saveDeviceValidate-->"+ JSON.toJSONString(deviceValidate));
        if(deviceValidate!=null ){
            deviceValidate.setModifyTime(DateUtil.DateToString(new Date(), DateStyle.YYYY_MM_DD_HH_MM));
            deviceValidate.setModifyId(employeeVo.getEmployeeId());
           // 校验记录
            if(StringUtils.isEmpty(deviceValidate.getDeviceValidateId())){
                DeviceVO deviceVO =  jsmDeviceService.getDeviceById(deviceValidate.getDeviceId());
                deviceValidate.setDeviceValidateId(UniqueUtil.uuid());
                deviceValidate.setCreateId(employeeVo.getEmployeeId());
                deviceValidate.setCreateTime(DateUtil.DateToString(new Date(), DateStyle.YYYY_MM_DD_HH_MM));
                int flag =  deviceValidateMapper.insertSelective(deviceValidate);
                // 判断是否操作成功
                if(flag > 0){
                    Device device = new Device();
                    device.setDeviceId(deviceValidate.getDeviceId());
                    device.setModifyTime(DateUtil.DateToString(new Date(),DateStyle.YYYY_MM_DD_HH_MM));
                    device.setModifyId(employeeVo.getEmployeeId());
                    device.setLastValidateTime(deviceValidate.getValidateTime());
                    List<Time> timeList = new ArrayList<>();
                    if(deviceVO.getDeviceValidateCycleList() != null){
                        for(DeviceValidateCycle deviceValidateCycle : deviceVO.getDeviceValidateCycleList()){
                                 Time time = new Time();
                                 time.setLv(Integer.parseInt(deviceValidateCycle.getValidatePv()+""));
                                 time.setName(deviceValidateCycle.getValidateName());
                                 timeList.add(time);
                        }
                    }
                    Date date = TimeCalc.calc(deviceValidate.getValidateTime(),timeList) ;
                    device.setNextValidateTime(DateUtil.DateToString(date,DateStyle.YYYY_MM_DD_HH_MM));
                    deviceMapper.update(device);
                    valdateMap.put("state", RespCodeEnum.RESP_CODE_ENUM_true.code());
                    valdateMap.put("msg", BackMessage.ADD_OK);
                }else{
                    valdateMap.put("state", RespCodeEnum.RESP_CODE_ENUM_false.code());
                    valdateMap.put("msg", BackMessage.ADD_FAIL);
                }

            }else{
                //  // 判断是否修改成功
                int flag =  deviceValidateMapper.updateByPrimaryKeySelective(deviceValidate);
                if(flag > 0){
                    valdateMap.put("state", RespCodeEnum.RESP_CODE_ENUM_true.code());
                    valdateMap.put("msg", BackMessage.MODIFY_OK);
                }else{
                    valdateMap.put("state", RespCodeEnum.RESP_CODE_ENUM_false.code());
                    valdateMap.put("msg", BackMessage.MODIFY_FAIL);
                }
            }
        }else{
            valdateMap.put("state", RespCodeEnum.RESP_CODE_ENUM_false.code());
            valdateMap.put("msg", BackMessage.ADD_FAIL);
            logger.info("校验记录保存：saveDeviceValidate-->记录为空"+ JSON.toJSONString(deviceValidate));
        }
        return valdateMap ;
    }

    /**
     * @param deviceId 根据设备id , 查询设备的校验记录
     * @return
     * @throws Exception
     */
    @Override
    public List<DeviceValidate> queryDeviceValidate(String deviceId) throws Exception {
        List<DeviceValidate> deviceValidateList = deviceValidateMapper.selectDeviceValidateList(deviceId);
        return deviceValidateList ;
    }

    /**
     * @param deviceValidate
     * 校验记录，列表展示
     */
    @Override
    public JSONObject queryDeviceValidateTable(DeviceValidate deviceValidate) throws Exception {
        JSONObject jsonObject = new JSONObject();
        Integer integer =  deviceValidateMapper.queryCount(deviceValidate);
        List<DeviceValidate> deviceValidateList = deviceValidateMapper.queryList(deviceValidate);
        jsonObject.put("total",integer);
        jsonObject.put("rows",JSON.toJSON(deviceValidateList));
        return jsonObject ;
    }


}
